package com.hudongwx.sadpw.activity;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.hudongwx.sadpw.R;
import com.hudongwx.sadpw.fragment.foodmenu.HaoGuoFragment;
import com.hudongwx.sadpw.fragment.foodmenu.HaoJFragment;
import com.hudongwx.sadpw.fragment.foodmenu.HaoJiangFragment;
import com.hudongwx.sadpw.fragment.foodmenu.HaoJiuFragment;
import com.hudongwx.sadpw.fragment.foodmenu.HaoMianFragment;
import com.hudongwx.sadpw.fragment.foodmenu.HaoYouFragment;

public class FoodMenuActivity extends AppCompatActivity {
    HaoGuoFragment haoGuoFragment;
    HaoJFragment haoJFragment;
    HaoJiangFragment haoJiangFragment;
    HaoJiuFragment haoJiuFragment;
    HaoMianFragment haoMianFragment;
    HaoYouFragment haoYouFragment;
    TabLayout tabLayout;
    ViewPager viewPager;
    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_menu);
        tabLayout= (TabLayout) findViewById(R.id.food_tablayout);
        viewPager= (ViewPager) findViewById(R.id.food_viewpager);
        imageView= (ImageView) findViewById(R.id.food_back);
        Myadapter adapter=new Myadapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        //设置tablayout和viewpager联动
        tabLayout.setupWithViewPager(viewPager);
        Intent intent = getIntent();
        int type = intent.getIntExtra("type", 0);
        viewPager.setCurrentItem(type);
        back();
    }
    public void back(){
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }
    class Myadapter extends FragmentStatePagerAdapter {
        public Myadapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    if (haoYouFragment==null){
                        haoYouFragment=new HaoYouFragment();
                    }
                    return haoYouFragment;
                case 1:
                    if (haoMianFragment==null){
                        haoMianFragment=new HaoMianFragment();
                    }
                    return haoMianFragment;
                case 2:
                    if(haoGuoFragment==null){
                        haoGuoFragment=new HaoGuoFragment();
                    }
                    return haoGuoFragment;
                case 3:
                    if (haoJiangFragment==null){
                        haoJiangFragment=new HaoJiangFragment();
                    }
                    return haoJiangFragment;
                case 4:
                    if (haoJiuFragment==null){
                        haoJiuFragment=new HaoJiuFragment();
                    }
                    return haoJiuFragment;
                case 5:
                    if (haoJFragment==null){
                        haoJFragment=new HaoJFragment();
                    }
                    return haoJFragment;
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 6;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position){
                case 0:
                    return "好油";
                case 1:
                    return "好面";
                case 2:
                    return "好果";
                case 3:
                    return "好浆";
                case 4:
                    return "好酒";
                case 5:
                    return "好酱";
                default:
                    return null;
            }
        }
    }
}
